public class ReverseList {
    public static class ListNode{
        int val;
        ListNode next;
        ListNode(){}
        ListNode(int val){
            this.val = val;
        }
    }
    //使用迭代法
    public ListNode reverseList(ListNode head){
        ListNode newhead = null;
        while (head != null){
            ListNode next = head.next;//记住head的下一个节点
            head.next = newhead;
            newhead = head;
            head = next;
        }
        return newhead;
    }


    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(45);
        ListNode listNode3 = new ListNode(23);
        ListNode listNode4 = new ListNode(90);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        ReverseList r = new ReverseList();
        ListNode head = r.reverseList(listNode1);
        System.out.println(head.val);
    }
}
